package com.example.springboot.service.impl;

import com.example.springboot.entity.UserRole;
import com.example.springboot.mapper.UserRoleMapper;
import com.example.springboot.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserRoleServiceImpl implements UserRoleService {

    @Autowired
    public UserRoleMapper userRoleMapper;
    @Override
    public List<Long> getRoleIdByUserId(Long id) {
        List<UserRole> UserRoles = userRoleMapper.getByUserId(id);
        List<Long> roleIds = UserRoles.stream().map(item -> {
            return item.getRoleId();
        }).collect(Collectors.toList());
        return roleIds;
    }

    @Override
    public Boolean deleteAllRoleByUserId(Long userId) {
        userRoleMapper.deleteAll(userId);
        return true;
    }

    @Override
    public Boolean add(UserRole userRole) {
        userRoleMapper.add(userRole);
        return true;
    }

    @Override
    public Boolean update(UserRole userRole) {
        userRoleMapper.update(userRole);
        return true;
    }

    @Override
    public List<String> getUserNameByRoleId(Long id) {
        List<UserRole> UserRoles = userRoleMapper.getByRoleId(id);
        List<String> userNames = UserRoles.stream().map(item -> {
            return item.getUserName();
        }).collect(Collectors.toList());
        return userNames;
    }

    @Override
    public List<UserRole> getUserRoleByUserId(Long id) {
        List<UserRole> UserRoles = userRoleMapper.getByUserId(id);
        return UserRoles;
    }

}
